<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { indexSevenInfo } from '@/api/index'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.chart = echarts.init(this.$el, 'macarons')
    indexSevenInfo().then(response => {
      const res = response.result
      if (!res.sevenMap) {
        res.sevenMap = [0, 0, 0, 0, 0, 0, 0]
      }
      res.sevenMap.length = Object.keys(res.sevenMap).length
      this.setOptions(res.sevenList.reverse(), Array.from(res.sevenMap).reverse())
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOptions(xaxisData, yaxisData) {
      this.chart.setOption({
        xAxis: {
          data: xaxisData,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 20,
          right: 40,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          minInterval: 1,
          name: '单位/人',
          axisTick: {
            show: false
          }
        },
        series: [
          {
            name: '人数',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#3888fa',
                lineStyle: {
                  color: '#3888fa',
                  width: 2
                }
              }
            },
            data: yaxisData,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          }
        ]
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.box-card {
  height: 350px;
}
</style>
